// 
// AnalysisEngineService.cs
//  
// Author:
//       Nikhil Sarda <diff.operator@gmail.com>
// 
// Copyright (c) 2010 Nikhil Sarda
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;

using MonoDevelop.Ide;
using MonoDevelop.Ide.Gui;

namespace MonoDevelop.FreeSharper
{
	public class AnalysisEngineService : IDisposable
	{
		public static Dictionary<string, RuleExtension> AnalysisRules = new Dictionary<string, RuleExtension>();
		public static IEnumerable<RuleExtension> ValidRules(Document d)
		{
			lock(analysisEngineLock)
				foreach(var rule in AnalysisRules) {
					if(rule.Value.IsValidForDocument)
						yield return rule.Value;
				}
		}
		
		public static void RegisterRule(RuleExtension rule)
		{
			if(rule!=null) {
				lock(analysisEngineLock)
					if(!AnalysisRules.ContainsKey(rule.RuleId))
						AnalysisRules.Add(rule.RuleId, rule);
			}
		}
		
		public static IEnumerable<RuleExtension> GetRegisteredRules()
		{
			lock(analysisEngineLock)
				foreach(var rule in AnalysisRules) {
					yield return rule.Value;
			}
		}
		
		internal static object analysisEngineLock = new object();
		
		Queue<AbstractAnalysisTask> analysisTaskQueue = new Queue<AbstractAnalysisTask>();
 		EventWaitHandle engineWaitHandle = new AutoResetEvent (false);
  		Thread analysisThread;

		bool disposed;
		
		public AnalysisEngineService() 
		{
			disposed = false;
			analysisThread = new Thread (Work);
   			analysisThread.Start();
		}
		
		public void EnqueueTask (AbstractAnalysisTask d) 
		{
	   		lock (analysisEngineLock) 
			{
				//TODO Need a policy to enqueue 
				analysisTaskQueue.Enqueue (d);
				if(analysisTaskQueue.Count == 1)
					engineWaitHandle.Set();
			}
		}
	
		public void Dispose () 
		{
			if(disposed!=false)
				disposed = false;
	    	EnqueueTask (null);     
	   		analysisThread.Join();    
	   		engineWaitHandle.Close();
	  	}

		public void Flush ()
		{
			lock (analysisEngineLock)
			{
				if(analysisTaskQueue.Count > 0)
					analysisTaskQueue.Clear();
			}
		}
		
		void Work() 
		{
	   		while (!disposed) {
		    	AbstractAnalysisTask task = null;
		    	lock (analysisEngineLock)
				if (analysisTaskQueue.Count > 0) {
		     		task = analysisTaskQueue.Dequeue();
					if (task == null) return;
				}
		
			    if (task != null) {
			    	//Perform task
					task.ExecuteTask();
					DispatchService.GuiSyncDispatch(delegate { 
						task.PostOnTextEditor();
					});	
				} else {
			        	engineWaitHandle.WaitOne();
				}
			}
		}
	}
}
